package cn.elane.kibana.demo.core.index.biz;

import cn.elane.kibana.demo.global.model.NameBean;
import cn.elane.kibana.demo.global.model.NameBeanUtil;
import cn.elane.kibana.demo.global.render.R;
import cn.elane.kibana.demo.global.util.HttpUtil;
import cn.elane.kibana.demo.global.util.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ApiBiz {

	@Value("${application.url}")
	private String url;

	public R getEnv(NameBean param) {
		if (param == null || param.isEmpty()) {
			return R.fail("参数不能为空!");
		}
		Object s = HttpUtil.get(url + "/api/apm/ui_filters/environments?start=" + param.str("start") + "&end=" + param.str("end"));
		return R.success(s);
	}

	public R getParam(NameBean param) {
		if (param == null || param.isEmpty()) {
			return R.fail("参数不能为空!");
		}
		String s = HttpUtil.get(url
				+ "/api/apm/ui_filters/local_filters/services?start="
				+ param.str("start")
				+ "&end=" + param.str("end")
				+ "&uiFilters=" + param.str("uiFilters")
				+ "&filterNames=" + param.str("filterNames")
		);
		List<NameBean> rs = NameBeanUtil.init2List(s);
		return R.success(rs);
	}

	public R list(NameBean param) {
		if (param == null || param.isEmpty()) {
			return R.fail("参数不能为空!");
		}
		String s = HttpUtil.get(url + "/api/apm/services?start="
				+ param.str("start") + "&end="
				+ param.str("end") + "&uiFilters="
				+ param.str("uiFilters"));
		return R.success(new NameBean(s));
	}

	public R tracesList(NameBean param) {
		if (param == null || param.isEmpty()) {
			return R.fail("参数不能为空!");
		}
		String s = HttpUtil.get(url + "/api/apm/traces?start="
				+ param.str("start") + "&end="
				+ param.str("end") + "&uiFilters="
				+ param.str("uiFilters"));
		return R.success(new NameBean(s));
	}

	public R distribution(String start, String end, String uiFilters, String transactionType, String transactionName, String serviceName) {
		if (start == null || "".equals(start) || end == null || "".equals(end)) {
			return R.fail("起始时间不能为空!");
		}
		String s = HttpUtil.get(url + "/api/apm/services/"
				+ serviceName
				+ "/transactions/charts/distribution?start=" + start
				+ "&end=" + end
				+ "&uiFilters=" + uiFilters
				+ "&transactionType=" + transactionType
				+ "&transactionName=" + transactionName);
		return R.success(new NameBean(s));
	}

	public R listById(NameBean param) {
		if (param == null || param.isEmpty()) {
			return R.fail("参数不能为空!");
		}
		String s = HttpUtil.get(url + "/api/apm/traces/" + param.str("traceId") + "?start="
				+ param.str("start") + "&end="
				+ param.str("end"));
		return R.success(new NameBean(s));
	}

	public R serviceMap(String start, String end, String environment) {
		if (start == null || "".equals(start) || end == null || "".equals(end)) {
			return R.fail("起始时间不能为空!");
		}
		String s = HttpUtil.get(url + "/api/apm/service-map?start=" + start
				+ "&end=" + end
				+ "&environment=" + environment);
		if (StringUtil.ifJSONObjectStr(s)) {
			return R.success(new NameBean(s));
		}
		return R.success(new NameBean(s));
	}
}
